﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows;

namespace HomeBudget
{
    public static class Database
    {
        public static void SaveToFile(List<Money> budgetItems)
        {
            StringBuilder textToSave = new StringBuilder();

            foreach (Money member in budgetItems)
            {
                if (member is IOneTimeItem)
                {
                    textToSave.AppendLine("onetime-" + member.ToString());
                }
                else if(member is IRecurringItem)
                {
                    textToSave.AppendLine("recurring-" + member.ToString());
                }
            }
            if (budgetItems[0] is Earning) //this budgetItems are Earning
            {
                StreamWriter saveWriter = new StreamWriter("...\\...\\IncomeDB.txt");
                using (saveWriter)
                {
                    saveWriter.Write(textToSave.ToString());
                }
            }
            else //budgetItems are Expense items
            {
                StreamWriter saveWriter = new StreamWriter("...\\...\\ExpenseDB.txt");
                using (saveWriter)
                {
                    saveWriter.Write(textToSave.ToString());
                }   
            }
        }
        private static List<Money> Load(string path, string moneyKind)
        {
            StreamReader reader = new StreamReader(path);
            string text = File.ReadAllText(path);

            if (text == string.Empty || text == null)
            {
                return new List<Money>();
            }

            List<Money> expenses = new List<Money>();
            using (reader)
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    string[] attributes = line.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
                    if (attributes[0] == "onetime")
                    {
                        if (moneyKind == "expense")
                        {
                            expenses.Add(new OneTimeExpense(decimal.Parse(attributes[1]), attributes[2],
                                new Category(attributes[3]), DateTime.ParseExact(attributes[4], "MM'/'dd'/'yyyy", CultureInfo.InvariantCulture)));
                        }
                        else
                        {
                            expenses.Add(new OneTimeEarning(decimal.Parse(attributes[1]), attributes[2],
                                new Category(attributes[3]), DateTime.ParseExact(attributes[4], "MM'/'dd'/'yyyy", CultureInfo.InvariantCulture)));
                        }
                    }
                    else
                    {
                        RecurringInterval interval;
                        if (attributes[4] == "weekly") { interval = RecurringInterval.weekly; }
                        else if (attributes[4] == "year") { interval = RecurringInterval.year; }
                        else if (attributes[4] == "monthly") { interval = RecurringInterval.monthly; }
                        else if (attributes[4] == "biweekly") { interval = RecurringInterval.biweekly; }
                        else { interval = RecurringInterval.tenDays; }
                        if (moneyKind == "expense")
                        {
                            expenses.Add(new RecurringExpense(decimal.Parse(attributes[1]), attributes[2], new Category(attributes[3]),
                                interval));
                        }
                        else
                        {
                            expenses.Add(new RecurringEarning(decimal.Parse(attributes[1]), attributes[2], new Category(attributes[3]),
                                interval));
                        }
                    }
                }
            }
            return expenses;
        }
        public static List<Money> LoadFromExpenseFile()
        {
            string path = "...\\...\\ExpenseDB.txt";
            return Load(path, "expense");
        }
        public static List<Money> LoadFromIncomeFile()
        {
            string path = "...\\...\\IncomeDB.txt";
            return Load(path, "income");
        }
    }
}
